package progress.exa28_1.part3;

import progress.exa28_1.part2.PrintChar;

public class ThreadStudy {

    public static void main(String[] args) throws InterruptedException {
        //创建任务
        Runnable printA=new PrintChar('a',5);
        Runnable printB=new PrintChar('b',5);

        //新建线程
        Thread thread1 = new Thread(printA);
        Thread thread2 = new Thread(printB);
        //理论上优先级值越小, 执行越优先
        thread1.setPriority(10);
        thread2.setPriority(1);
        //并行开始运行thread1,thread2
        thread1.start();
        thread2.start();

        //等待thread1,thread2执行结束
        thread1.join();
        thread2.join();
        System.out.println();

        /**
         * 利用isAlive对线程状态进行检查
         */
        while (true){

            //让当前线程进入休眠状态
            Thread.sleep(1000);
            boolean thread1Alive = thread1.isAlive();
            boolean thread2Alive = thread2.isAlive();
            if(!thread1Alive&&!thread2Alive){
                System.out.println("thread1 和 thread2 运行完毕！");
                break;
            }
            if(thread1Alive) System.out.println("thread1 正在运行");
            if(thread2Alive) System.out.println("thread2 正在运行");

        }

    }



}
